%% meas para
tableList = {'Bat1Table.csv'};%'BatFLITable.csv','HumanTable.csv'};%'Bat2Table.csv'};
channelWidth = 20; % um
pixSize = 0.34; % um/pix

exportRootDir = 'exportForECalc';

%% find samples

% load TDMS lib
WriteTDMSBegin

for idxTableList = 1:length(tableList)
    fprintf('Table: %s\n', tableList{idxTableList});
    tableComplete = readtable(tableList{idxTableList});
    
    [~,fileName,~] = fileparts(tableList{idxTableList});
    
    tableComplete.index = categorical(tableComplete.index);
    indexLevels = categories(tableComplete.index);
    tableComplete.tempLevel = categorical(tableComplete.tempLevel);
    tempLevels = categories(tableComplete.tempLevel);
    fprintf('AnimalIndex\tTempLevel\tEvents\n');
    tableEventCounts = array2table(zeros(0,3));
    tableEventCounts.Properties.VariableNames = {'AnimalIndex','TempLevel', 'Events'};
    for idxIndexLevels = 1:length(indexLevels)
        indexLevel = indexLevels{idxIndexLevels};
        for idxTempLevels = 1:length(tempLevels)
            tempLevel = tempLevels{idxTempLevels};
            
            mask = (tableComplete.index == indexLevel) & (tableComplete.tempLevel == tempLevel);
            tableCondition = tableComplete(mask,:);
            fprintf('%s\t\t\t%s\t%d\n', indexLevel, tempLevel, height(tableCondition));
            tableEventCounts = [tableEventCounts; {indexLevel, tempLevel, height(tableCondition)}];
            
            timeOutlet = tableCondition.measNum * 1e5 + tableCondition.cellNum;
            xOutlet = ones(height(tableCondition),1);
            yOutlet = ones(height(tableCondition),1);
            ax1Outlet = ones(height(tableCondition),1);
            ax2Outlet = ones(height(tableCondition),1);
            circDeltaTau2 = 1 - tableCondition.deltaDefoTau2;
            areaOutlet = tableCondition.area;
            rawAreaOutlet = tableCondition.area;
            
            targetDir = [exportRootDir filesep fileName filesep tempLevel];
            if ~exist(targetDir,'dir')
                mkdir(targetDir);
            else
%                 delete([targetDir filesep '*']);
            end
            targetName = ['M' indexLevel '_2us_70A_0.008000ul_s' '.tdms'];
            
            subExportTDMSForECalc
            
            %% copy camera & para file
            fileCamera = dir([exportRootDir filesep 'DummyFiles' filesep 'M' 'Dummy' '_camera.ini']);
            filePara = dir([exportRootDir filesep 'DummyFiles' filesep 'M' 'Dummy' '_para.ini']);
            fileCntr = dir([exportRootDir filesep 'DummyFiles' filesep 'M' 'Dummy' '_contours.txt']);
            fileAvi = dir([exportRootDir filesep 'DummyFiles' filesep 'M' 'Dummy' '_imaq.avi']);
            
            copyfile(fullfile(fileCamera.folder,fileCamera.name),fullfile(targetDir,['M' indexLevel '_camera' '.ini']));
            copyfile(fullfile(filePara.folder,filePara.name),fullfile(targetDir,['M' indexLevel '_para' '.ini']));
            copyfile(fullfile(fileCntr.folder,fileCntr.name),fullfile(targetDir,['M' indexLevel '_0.008000ul_s_contours' '.txt']));
            copyfile(fullfile(fileAvi.folder,fileAvi.name),fullfile(targetDir,['M' indexLevel '_0.008000ul_s_imaq' '.avi']));
            
        end
    end
    writetable(tableEventCounts,[exportRootDir filesep fileName filesep 'eventCounts.csv']);
end

% unload TDMS lib
WriteTDMSEnd